package com.xywy.tangandroid.me.activity;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.xywy.tangandroid.R;
import com.xywy.tangandroid.base.BaseActivity;
import com.xywy.tangandroid.bean.FeedBack;
import com.xywy.tangandroid.bean.User;
import com.xywy.tangandroid.util.Constant;
import com.xywy.tangandroid.util.MD5;
import com.xywy.tangandroid.util.UIUtil;
import com.xywy.tangandroid.vollery.OnVolleyResponseListener;
import com.xywy.tangandroid.vollery.PostRequest;

/*******************************************
 * @Title: SetAboutSugarDoctorFeedBackActivity.java
 * @Package com.xywy.tangandroid.me.activity
 * @Description:意见反馈
 * @author Benwolf
 * @date 2014-12-3 下午6:05:24
 * @version V1.0
 ********************************************/
public class SetAboutSugarDoctorFeedBackActivity extends BaseActivity {

	private Button btn_feedback;
	private EditText content_feedback;
	private OnFocusChangeListener mOnFocusChangeListener = new OnFocusChangeListener() {
		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			EditText textView = (EditText) v;
			String hint;
			if (hasFocus) {
				hint = textView.getHint().toString();
				textView.setTag(hint);
				textView.setHint("");
			} else {
				hint = textView.getTag().toString();
				textView.setHint(hint);
			}
		}
	};
	private RelativeLayout left_img;
	private TextView middle_title;

	@Override
	protected void initView(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_set_about_sugardoctor_feedback);
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText("意见反馈");
		content_feedback = (EditText) findViewById(R.id.content_feedback);
		content_feedback.setOnFocusChangeListener(mOnFocusChangeListener);
		btn_feedback = (Button) findViewById(R.id.btn_feedback);
	}

	@Override
	protected void initListener() {
		// TODO Auto-generated method stub
		left_img.setOnClickListener(this);
		btn_feedback.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		super.onClick(view);
		switch (view.getId()) {
		case R.id.left_img:
			finish();
			break;
		case R.id.btn_feedback:
			String content = content_feedback.getText().toString().trim();
			submit(content);
			break;
		default:
			break;
		}
	}

	private void submit(String content) {
		// TODO Auto-generated method stub
		String sign = null;
		try {
			sign = MD5.md5s(Constant.Sign + Constant.tag);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		String url = "http://api.wws.xywy.com/index.php?" + "&tag="
				+ Constant.tag + "&sign=" + sign + "&act=" + "kbb" + "&fun="
				+ "user_feedback";
		PostRequest<String> postRequest = new PostRequest<String>(url,
				String.class, new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						UIUtil.DissDialog(context, waittingDialog);
						Toast.makeText(context, "提交失败！", Toast.LENGTH_SHORT)
								.show();

					}

					@Override
					public void onSuccess(String response) {
						UIUtil.DissDialog(context, waittingDialog);
						if (response != null) {
							JSONObject jsonObject;
							try {
								jsonObject = new JSONObject(response);
								String state = jsonObject.getString("state");
								if (state.equals("200")) {
									Toast.makeText(context, "提交成功！",
											Toast.LENGTH_SHORT).show();
									finish();
								}
							} catch (JSONException e) {
								e.printStackTrace();
							}

						}
					}
				});

		HashMap<String, String> params = new HashMap<String, String>();
		params.put("userid", User.getIUserInstance(this).getUserid());// userid
		params.put("apptype", "10004");// apptype
		try {
			params.put("content", URLEncoder.encode(content, "UTF-8")
					.toString());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		postRequest.setIsParseJson(false);
		postRequest.setParams(params);
		executeRequest(postRequest);

	}
	@Override
	protected void initData() {
		// TODO Auto-generated method stub

	}
}